package com.clark.crawler.novel.pipeline;

import com.clark.crawler.novel.NovelUtil;
import com.clark.crawler.novel.bean.Chapter;
import com.clark.crawler.novel.deal.ChapterDeal;
import com.clark.crawler.novel.factory.IChapterDealFactory;
import com.clark.crawler.novel.property.NovelPropertyUtil;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

/**
 * description:
 *
 * @author tianhua.xie（tianhua.xie@ucarinc.com）
 * @version 1.0
 * @date 2018/12/26 10:29
 */
public class ChapterPipeline implements Pipeline {
    private static ChapterDeal chapterDeal;
    static {
        try {
            Class<?> c = Class.forName(NovelPropertyUtil.getChapterDealFactoryClassName());
            IChapterDealFactory novelDealFactory = (IChapterDealFactory) c.getDeclaredConstructor().newInstance();
            chapterDeal = novelDealFactory.getInstance();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("配置初始化失败");
        }
    }

    @Override
    public void process(ResultItems resultItems, Task task) {
        Chapter chapter = resultItems.get("chapter");
        chapterDeal.deal(chapter);
        NovelUtil.addChapter(chapter);
    }
}
